<?php

$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
set_include_path(get_include_path().PATH_SEPARATOR.$baseDir.PATH_SEPARATOR.$baseDir.'/incubator');

include '../../testingfunctions.php';
include 'Pone/Pone_Exception.php';
include 'Pone/Database/Pone_Database_ConnectionFactory.php';
include_once './dbconfig.php'; // $config is defined here
include_once './test_setup.php'; // refreshTestData() is defined here

try
{
    refreshTestData($config);
    // /home/web/windows.php.net/docroot/downloads/snaps/
    $conn = Pone_Database_ConnectionFactory::getConnection($config);
    $stmt = $conn->createStatement();
    $rs   = $stmt->fetchPage('SELECT * FROM test2 ORDER BY seq_number ASC', 1, 2);
    assertTrue(is_array($rs), 'Expected: An array is returned. ');
    echo '<br />';
    assertTrue((count($rs) === 2), 'Expected: Return array contains 2 elements. ');
    echo '<br />';
    $cond = isset($rs[0]['id']) && isset($rs[0]['username']) && isset($rs[0]['seq_number']) &&
    isset($rs[1]['id']) && isset($rs[1]['username']) && isset($rs[1]['seq_number']);
    assertTrue($cond, 'Expected: Each child element in return array contains 3 fields: id, username and seq_number. ');
    echo '<br />';
    assertTrue(isset($rs[0]['seq_number']) && $rs[0]['seq_number'] === '1', 'Expected: The value of "seq_number" field in the first row in the return array must be 1. ');
    echo '<br />';
    assertTrue(isset($rs[1]['seq_number']) && $rs[1]['seq_number'] === '2', 'Expected: The value of "seq_number" field in the second row in the return array must be 2. ');

    // Free resources
    $stmt->close();
}
catch (Pone_Exception_DatabaseAccess $ex)
{
    echo implode('. ', $ex->getTraceMessages());
    echo '<br />';
    echo $ex->getNativeMessage();
}
catch (Exception $ex)
{
    echo $ex->getMessage();
}

$conn->close();
?>